﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class LoadTrace : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
            //    SetTreeNode();
            //}

        }

        #region 顺序Load节点

        /// <summary>
        /// 树节点绑定
        /// </summary>
        public void SetTreeNode()
        {
            TreeNodeCollection loadList = GetLoadNode(txtLoadNum.Text);
            foreach (TreeNode loadNode in loadList)
            {
                
                treeLoad.Nodes.Add(loadNode);
            }
        }

        /// <summary>
        /// 获取Load节点 根节点
        /// </summary>
        public TreeNodeCollection  GetLoadNode(string loadNum)
        {
            TreeNodeCollection loadLoadList = new TreeNodeCollection();

            DataTable dt = new DataLib.BLL.LoadNum().GetList("loadnum='" + loadNum.Trim() + "'").Tables[0];


            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode loadLoad = new TreeNode();
                    loadLoad.ToolTip = "";     //鼠标移上去的提示
                    loadLoad.Text = "LoadNum: " + dt.Rows[i]["loadnum"].ToString();
                    loadLoad.Value = dt.Rows[i]["id"].ToString();
                    GetCjoNode(loadLoad);
                    loadLoadList.Add(loadLoad);
                }
            }

            return loadLoadList;
        }

        /// <summary>
        /// 获取CJO节点
        /// </summary>
        public void GetCjoNode(TreeNode loadNode)
        {

            DataTable dt = new DataLib.BLL.Cjo().GetList("loadnumid=" + loadNode.Value).Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode CjoNode = new TreeNode();
                    CjoNode.Text = "CJO No:" + dt.Rows[i]["cjono"].ToString();
                    CjoNode.Value = dt.Rows[i]["id"].ToString();
                    CjoNode.ToolTip = "";
                    GetWrlNode(CjoNode);
                    loadNode.ChildNodes.Add(CjoNode);
                }
            }
        }

        /// <summary>
        /// 获取称重节点
        /// </summary>
        public void GetWrlNode(TreeNode cjoNode)
        {
          
            DataTable dt = new DataLib.BLL.InStorage().GetList("cjoid=" + cjoNode.Value + " and outid=0").Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode wrlNode = new TreeNode();
                    wrlNode.Text = "称重：" + dt.Rows[i]["wrlno"].ToString();
                    wrlNode.Value = dt.Rows[i]["id"].ToString();
                    wrlNode.ToolTip = "";
                    GetBarByIns(wrlNode);
                    cjoNode.ChildNodes.Add(wrlNode);
                }
            }

           
        }

        /// <summary>
        /// 获取入库单下的BarCode节点
        /// </summary>
        public void GetBarByIns(TreeNode insNode)
        {
 
            DataTable dt = new DataLib.BLL.BarCode().GetList("InStrorageID=" + insNode.Value).Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode barlNode = new TreeNode();
                    barlNode.Text = dt.Rows[i]["goodsname"].ToString();
                    barlNode.Value = dt.Rows[i]["id"].ToString();
                    barlNode.ToolTip = dt.Rows[i]["barcodeValue"].ToString() + "  " + dt.Rows[i]["goodscode"].ToString();
                    GetOutNode(barlNode);
                    insNode.ChildNodes.Add(barlNode);
                }
            }

        }

        /// <summary>
        /// 获取出库节点
        /// </summary>
        public void GetOutNode(TreeNode barNode)
        {
 
            DataTable dt = new DataLib.BLL.OutStorage().GetList("id in (select OutStrorageID  from barcode where id= " + barNode.Value + ")").Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode outsNode = new TreeNode();
                    outsNode.Text = "出库：" + dt.Rows[i]["outStorageNo"].ToString();
                    outsNode.Value = dt.Rows[i]["id"].ToString();
                    //outsNode.ToolTip = dt.Rows[i]["goodscode"].ToString() + "  " + dt.Rows[i]["goodsname"].ToString();
                    GetInsNode(outsNode);
                    barNode.ChildNodes.Add(outsNode);
                }
            }

        }

        /// <summary>
        /// 获取入库节点
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public void GetInsNode(TreeNode outNode)
        {
           

            DataTable dt = new DataLib.BLL.InStorage().GetList("outid=" + outNode.Value ).Tables[0];

            if (dt != null && dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode insNode = new TreeNode();
                    insNode.Text = "入库：" + dt.Rows[i]["wrlno"].ToString();
                    insNode.Value = dt.Rows[i]["id"].ToString();
                    insNode.ToolTip = "";
                    GetBarByIns(insNode);
                    outNode.ChildNodes.Add(insNode);
                }
            }

           
        }

        protected void BtnTrace_Click(object sender, EventArgs e)
        {
            if (txtLoadNum.Text.Trim() != "")
            {
                treeLoad.Nodes.Clear();
                SetTreeNode();
            }
            else
            {
                DataLib.Common.MessageBox.Show(Page, "请输入LoadNum号");
            }
        }

        ///// <summary>
        ///// 从barcode开始递归
        ///// </summary>
        ///// <param name="parentNode">BarCode节点</param>
        //public void CreateChildNode(TreeNodeCollection parentNode)
        //{
        
        //    foreach (TreeNode parNode in parentNode)
        //    {
        //        List<TreeNode> outList = GetOutNode(parNode.Value);

        //        foreach (TreeNode outsNode in outList)
        //        {
        //            List<TreeNode> insList = GetInsNode(outsNode.Value);
        //            foreach (TreeNode insLoad in insList)
        //            {
        //                 List<TreeNode> barList = GetBarByIns(insLoad.Value);
        //                foreach (TreeNode barNode in barList)
        //                {
        //                    insLoad.ChildNodes.Add(barNode);
        //                }
        //                outsNode.ChildNodes.Add(insLoad);
        //                //CreateChildNode( barList);
                       
        //            }
        //            parNode.ChildNodes.Add(outsNode);

        //        }
        //    }
         
        //}
        #endregion
    }
}